﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PlasmaMes.A_Models;
using PlasmaMes.Services;

namespace PlasmaMes
{
    public partial class Form_PLC: Form
    {
        public Form_PLC()
        {
            InitializeComponent();
        }

        private void Form_PLC_Load(object sender, EventArgs e)
        {
            uiComboBox_写入类型.SelectedIndex = 0;
            uiComboBox_读取类型.SelectedIndex = 0;

            tb_ip.Text = RunParam.Instance.PLC_IP;
        }

        private void uiButton_连接_Click(object sender, EventArgs e)
        {
            if(Modbus.Instance.ConnectModbus(tb_ip.Text))
            {
                MessageBox.Show("与本机台PLC连接成功");
            }
            else
            {
                MessageBox.Show("与本机台PLC连接失败");
            }
        }

        private void uiButton_断开_Click(object sender, EventArgs e)
        {
            Modbus.Instance.DisConnectMudbus();
        }

        private void bt_读取_Click(object sender, EventArgs e)
        {
            if(uiComboBox_读取类型.SelectedIndex == -1)
            {
                MessageBox.Show("读取类型不能为空");
                return;
            }

            if(string.IsNullOrEmpty(tb_读取地址.Text))
            {
                MessageBox.Show("读取地址不能为空");
                return;
            }

            if(uiComboBox_读取类型.SelectedIndex == 0)
            {
                int value = 0;
                Modbus.Instance.ReadRegister_Int16(tb_读取地址.Text, ref value);

                this.BeginInvoke((MethodInvoker)delegate {
                    uiRichTextBox_读取.Clear();
                    uiRichTextBox_读取.Text = value.ToString();

                });
            }
            else if(uiComboBox_读取类型.SelectedIndex == 1)
            {
                float value = 0;
                Modbus.Instance.ReadRegister_Float(tb_读取地址.Text, ref value);

                this.BeginInvoke((MethodInvoker)delegate {

                    uiRichTextBox_读取.Clear();
                    uiRichTextBox_读取.Text = value.ToString();
                });
            }
        }

        private void bt_写入_Click(object sender, EventArgs e)
        {
            if (uiComboBox_写入类型.SelectedIndex == -1)
            {
                MessageBox.Show("写入类型不能为空");
                return;
            }

            if (string.IsNullOrEmpty(tb_写入地址.Text))
            {
                MessageBox.Show("写入地址不能为空");
                return;
            }

            if(string.IsNullOrEmpty(uiRichTextBox_写入.Text))
            {
                MessageBox.Show("写入内容不能为空");
                return;
            }

            if(uiComboBox_写入类型.SelectedIndex == 0)
            {
                short value = short.Parse(uiRichTextBox_写入.Text);
                if(Modbus.Instance.WriteOneRegister_Short(tb_写入地址.Text, value))
                {
                    MessageBox.Show("写入成功");
                    return;
                }
                
            }
            else
            {
                float value = float.Parse(uiRichTextBox_写入.Text);
                if (Modbus.Instance.WriteRegister_Float(tb_写入地址.Text, value))
                {
                    MessageBox.Show("写入成功");
                    return;
                }
            }
        }

        private void bt_保存参数_Click(object sender, EventArgs e)
        {
            RunParam.Instance.PLC_IP = tb_ip.Text;
            JsonHelper.ObjToJsonFile(RunParam.Instance, RunParam.Instance.RunParamPath);

            MessageBox.Show("保存参数成功");
        }
    }
}
